// 新增分类
function addArticleCategoryFun() {
    $("#articleCategoryFirstForm").hide();
    $("#articleCategorySecondForm").hide();
    // 判断类型
    $(".settingCategoryType").on("click", function () {
        var type = $(".settingCategoryType:checked").val();
        if (type == 0) {
            $("#articleCategoryFirstForm").show();
            $("#articleCategorySecondForm").hide();
        }
        // 子分类，加载所有父分类
        if (type == 1) {
            $("#articleCategorySecondForm").show();
            $("#articleCategoryFirstForm").hide();
            // 加载主分类
            $.ajax({
                type: "POST",
                url: "/articleCategoryManage/selectArticleCategoryFirst",
                dataType: "json",
                success: function (data) {
                    $(".articleCategorySon").empty();
                    var son = data.articleCategoryList;
                    for (var i = 0; i < son.length; i++) {
                        $(".articleCategorySon").append('<label class="checkbox-inline"><input type="radio" name="articleCategoryParentId" class="articleCategoryParentId" value="' + son[i].articleCategoryId + '"> ' + son[i].articleCategoryTitle + '</label>');
                    }
                },
                error: function () {
                    $(".articleCategorySon").html("服务器错误，加载文章次分类失败").css({"color": "red"});
                }
            });
        }
    });
    // 请求
    $("#addArticleCategoryBtn").on("click", function () {
        var type = $(".settingCategoryType:checked").val();
        if (typeof type == "undefined") {
            alert("请选择新增类型");
        }
        // 主分类
        if (type == 0) {
            var articleCategoryParentId = 0;
            var articleCategoryState = $(".articleCategoryState0:checked").val();
            var articleCategoryTitle = $(".articleCategoryTitle0").val();
            var articleCategoryIcon = $(".articleCategoryIcon0").val();
            var articleCategoryIntroduce = $(".articleCategoryIntroduce0").val();
            if (articleCategoryState != null && articleCategoryState.length != 0
                && articleCategoryTitle != null && articleCategoryTitle.length != 0
                && articleCategoryIcon != null && articleCategoryIcon.length != 0
                && articleCategoryIntroduce != null && articleCategoryIntroduce.length != 0) {
                $.ajax({
                    type: "POST",
                    url: "/articleCategoryManage/addArticleCategory",
                    dataType: "json",
                    data: {
                        articleCategoryParentId: articleCategoryParentId,
                        articleCategoryState: articleCategoryState,
                        articleCategoryTitle: articleCategoryTitle,
                        articleCategoryIcon: articleCategoryIcon,
                        articleCategoryIntroduce: articleCategoryIntroduce
                    },
                    success: function (data) {
                        if (data.result == "success") {
                            alert("新增成功");
                            window.location.reload();
                        } else {
                            alert("新增失败");
                        }
                    },
                    error: function () {
                        alert("服务器错误，操作失败");
                    }
                });
            } else {
                alert("请输入或设置所有信息，不能留有空白");
            }
        }
        // 次分类
        if (type == 1) {
            var articleCategoryParentId = $(".articleCategoryParentId:checked").val();
            var articleCategoryState = $(".articleCategoryState1:checked").val();
            var articleCategoryTitle = $(".articleCategoryTitle1").val();
            var articleCategoryIcon = $(".articleCategoryIcon1").val();
            var articleCategoryIntroduce = $(".articleCategoryIntroduce1").val();
            console.log(articleCategoryParentId);
            if (articleCategoryParentId != null && articleCategoryParentId.length != 0
                && articleCategoryState != null && articleCategoryState.length != 0
                && articleCategoryTitle != null && articleCategoryTitle.length != 0
                && articleCategoryIcon != null && articleCategoryIcon.length != 0
                && articleCategoryIntroduce != null && articleCategoryIntroduce.length != 0) {
                $.ajax({
                    type: "POST",
                    url: "/articleCategoryManage/addArticleCategory",
                    dataType: "json",
                    data: {
                        articleCategoryParentId: articleCategoryParentId,
                        articleCategoryState: articleCategoryState,
                        articleCategoryTitle: articleCategoryTitle,
                        articleCategoryIcon: articleCategoryIcon,
                        articleCategoryIntroduce: articleCategoryIntroduce
                    },
                    success: function (data) {
                        if (data.result == "success") {
                            alert("新增成功");
                            window.location.reload();
                        } else {
                            alert("新增失败");
                        }
                    },
                    error: function () {
                        alert("服务器错误，操作失败");
                    }
                });
            } else {
                alert("请输入或设置所有信息，不能留有空白");
            }
        }
    });
}

// 查看分类，具备更新功能
function articleCategoryFun(articleCategoryId) {
    $.ajax({
        type: "GET",
        url: "/articleCategoryManage/getArticleCategory?articleCategoryId=" + articleCategoryId,
        dataType: "json",
        success: function (data) {
            var articleCategory = data.articleCategory;
            if (articleCategory != null) {
                $(".articleCategoryId").val(articleCategory.articleCategoryId);
                $(".articleCategoryParentId").val(articleCategory.articleCategoryParentId);
                $(".articleCategoryTitle").val(articleCategory.articleCategoryTitle);
                $(".articleCategoryIcon").val(articleCategory.articleCategoryIcon);
                $(".articleCategoryIntroduce").val(articleCategory.articleCategoryIntroduce);
                if (articleCategory.articleCategoryState == 0) {
                    $(".articleCategoryState").eq(0).attr("checked", "true");
                } else {
                    $(".articleCategoryState").eq(1).attr("checked", "true");
                }
            } else {
                alert("查询不到数据");
            }
        },
        error: function () {
            alert("服务器错误，操作失败");
        }
    });
    // 更新
    $("#updateArticleCategoryBtn").on("click", function () {
        var articleCategoryId = $(".articleCategoryId").val();
        var articleCategoryParentId = $(".articleCategoryParentId").val();
        var articleCategoryState = $(".articleCategoryState:checked").val();
        var articleCategoryTitle = $(".articleCategoryTitle").val();
        var articleCategoryIcon = $(".articleCategoryIcon").val();
        var articleCategoryIntroduce = $(".articleCategoryIntroduce").val();
        if (articleCategoryState != null && articleCategoryState.length != 0
            && articleCategoryTitle != null && articleCategoryTitle.length != 0
            && articleCategoryIcon != null && articleCategoryIcon.length != 0
            && articleCategoryIntroduce != null && articleCategoryIntroduce.length != 0) {
            $.ajax({
                type: "POST",
                url: "/articleCategoryManage/updateArticleCategory",
                dataType: "json",
                data: {
                    articleCategoryId: articleCategoryId,
                    articleCategoryParentId: articleCategoryParentId,
                    articleCategoryState: articleCategoryState,
                    articleCategoryTitle: articleCategoryTitle,
                    articleCategoryIcon: articleCategoryIcon,
                    articleCategoryIntroduce: articleCategoryIntroduce
                },
                success: function (data) {
                    if (data.result == "success") {
                        alert("更新成功");
                        window.location.reload();
                    } else {
                        alert("更新失败");
                    }
                },
                error: function () {
                    alert("服务器错误，操作失败");
                }
            });
        } else {
            alert("请输入或设置所有信息，不能留有空白");
        }
    });
}